# -*- coding: utf-8 -*-
# @Time    : 2021/11/15 22:10
# @Author  : super
import time
import pymysql
import json

route_list = [

]
def route(path):
	def decorator(func):
		route_list.append((path, func))
		print("route_list",route_list)
		def inner():
			status, response, data = func()
			return status, response, data
		return inner
	return decorator


@route("/index.html")
def index():
	status = "200 OK"
	response_header = [("Server", "PWS2.0")]
	data = ""
	# 获取股票信息数据
	conn = pymysql.connect(host = "localhost",
	                       port = 3306,
	                       user = "root",
	                       password = "123456",
	                       database = "stock_db",
	                       charset = "utf8")
	cursor = conn.cursor()
	sql = "SELECT * FROM info;"
	cursor.execute(sql)
	result = cursor.fetchall()
	for item in result:
		data+= """
				<tr>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%s</td>
                    <td><input type="button" value="添加" id="toAdd" name="toAdd" systemidvaule="000007"></td>
                   </tr> 
				""" % item

	cursor.close()
	conn.close()

	with open("../template/index.html", "r", encoding = "utf-8") as f:
		filedata = f.read()
	response_body = filedata.replace("{%content%}", data)
	return status, response_header, response_body

@route("/center.html")
def center():
	status = "200 OK"
	response_header = [("Server", "PWS2.0")]

	with open("../template/center.html", "r", encoding = "utf-8") as f:
		filedata = f.read()
	data = filedata.replace("{%content%}",time.ctime())
	return status, response_header, data

@route("/center_index.html")
def center():
	status = "200 OK"
	response_header = [("Server", "PWS2.0"),
	                   ("Content-Type", "text/html;charset=utf-8")]
	# 获取个人中心数据
	conn = pymysql.connect(host = "localhost",
	                       port = 3306,
	                       user = "root",
	                       password = "123456",
	                       database = "stock_db",
	                       charset = "utf8")
	cursor = conn.cursor()
	sql = """SELECT i.code, i.short, i.chg, 
	             i.turnover, i.price, i.highs, f.note_info 
	             FROM info AS i INNER JOIN focus AS f ON i.id = f.info_id;"""
	cursor.execute(sql)
	result = cursor.fetchall()
	data = [{
		"code" : item[0],
		"short" : item[1],
		"chg" : item[2],
		"turnover" : item[3],
		"price" : str(item[4]),
		"highs" : str(item[5]),
		"note_info" : item[6]
	} for item in result]
	data = json.dumps(data, ensure_ascii = False,indent = True)
	cursor.close()
	conn.close()

	return status, response_header, data


def not_func():
	status = "400 Not Found"
	response_header = [("Server", "PWS2.0")]
	data = "400 Not Found"
	return status, response_header, data

def handle_request(env):
	request_path = env["request_path"]
	print("接收到的动态请求资源：", request_path)
	for path, func in route_list:
		if path == request_path:
			result = func()
			return result
	else:
		result = not_func()
		return result